//
//  tableEmploye.m
//  testPlanning
//
//  Created by richard_local on 04/09/13.
//  Copyright (c) 2013 ierdre.com. All rights reserved.
//

#import "tableEmploye.h"
#import "myPickerView.h"
@interface tableEmploye ()

@end

@implementation tableEmploye
@synthesize myPopover,ButtonService,longPress;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    longPress = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.tableView addGestureRecognizer:longPress];
    NSLog(@"%@" ,longPress);

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
 
}
/**
 Source 
 http://stackoverflow.com/questions/11245043/drag-drop-between-view-controllers-contained-in-the-same-view-controller
 */
- (IBAction)longPress:(UIGestureRecognizer *)sender
{
    NSLog(@"LONG PRESS");
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        // figure out which item in the table was selected
        
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[sender locationInView:self.tableView]];
        if (!indexPath)
        {
            _inDrag = NO;
            return;
        }
        
        _inDrag = YES;
        
        // get the text of the item to be dragged
        
        //NSString *text = [NSString stringWithString:[[_objects objectAtIndex:indexPath.row] description]];
        NSString *text=@"test";
        // create item to be dragged, in this example, just a simple UILabel
        
        UIView *splitView = self.splitViewController.view;
        CGPoint point = [sender locationInView:splitView];
        UIFont *font = [UIFont systemFontOfSize:12];
        CGSize size = [text sizeWithFont:font];
        CGRect frame = CGRectMake(point.x - (size.width / 2.0), point.y - (size.height / 2.0), size.width, size.height);
        _draggedView = [[UILabel alloc] initWithFrame:frame];
        [_draggedView setFont:font];
        [_draggedView setText:text];
        [_draggedView setBackgroundColor:[UIColor clearColor]];
        
        // now add the item to the view
        
        [splitView addSubview:_draggedView];
    }
    else if (sender.state == UIGestureRecognizerStateChanged && _inDrag)
    {
        // we dragged it, so let's update the coordinates of the dragged view
        
        UIView *splitView = self.splitViewController.view;
        CGPoint point = [sender locationInView:splitView];
        _draggedView.center = point;
    }
    else if (sender.state == UIGestureRecognizerStateEnded && _inDrag)
    {
        // we dropped, so remove it from the view
        
        [_draggedView removeFromSuperview];
        
        // and let's figure out where we dropped it
        UISplitViewController *splitC = self.splitViewController;
        NSLog(@"--> %@",splitC.viewControllers[1]);
        
       UIView *detailView = ((UIViewController*) splitC.viewControllers[1]).view;
        CGPoint point = [sender locationInView:detailView];
        
        /*UIAlertView *alert;*/
        if ((CGRectContainsPoint(detailView.bounds, point))){
           UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"dropped in details view" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
            NSLog(@"%f %f",point.x,point.y);
            UITableView * tableH = ((UITableViewController *)splitC.viewControllers[1]).tableView;
            CGPoint point2;
            point2.x = point.x+self.view.frame.size.width;
            point2.y =point.y;
            CGPoint hitPoint = [self.parentViewController.view convertPoint:point2 toView:tableH];
            NSIndexPath *hitIndex = [tableH indexPathForRowAtPoint:hitPoint];
            NSLog(@"%f %f %@",hitPoint.x,hitPoint.y,hitIndex);
        }
/*       else
            alert = [[UIAlertView alloc] initWithTitle:@"dropped outside details view" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];*/

    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *CellIdentifier =@"employer";
    if (indexPath.row==0 ){
     CellIdentifier =@"service";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    if (indexPath.row==0 ) {
       ButtonService = (UIButton *)[cell viewWithTag:10];
        [ButtonService addTarget:self
                     action:@selector(presentSortPopover)
           forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}
//
- (IBAction)presentSortPopover {
    NSLog(@" TEST POPOVER");
    
    myPickerView
    *detailViewController = (myPickerView*)[[myPickerView alloc]  initWithNibName:@"myPickerView" bundle:nil];


    UIPickerView *myPickerView=(UIPickerView *)[detailViewController.view viewWithTag:10];
    [myPickerView setDelegate:self];
    //[myPickerView setDataSource:self];
    myPopover = [[UIPopoverController alloc] initWithContentViewController:detailViewController];
   // popover.delegate = self;
    
   myPopover.popoverContentSize = CGSizeMake(300, 216);
    [myPopover presentPopoverFromRect:self.ButtonService.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    

    

}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"Service %d",row] ;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   // textField.text = [dataArray objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  
        return 1;

}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
    return 5;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
